﻿<template>
    <el-dialog
        :title="!dataForm.id ? '新建' : '编辑'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center"
        lock-scroll
        width="600px"
    >
        <el-row class="">
            <el-form
                ref="elForm"
                :model="dataForm"
                size="small"
                label-width="100px"
                label-position="right"
                :rules="rules"
            >
                <el-col :span="24">
                    <el-form-item label="检验项目" prop="innspectionName">
                        <SHZY-select
                            :list="innspectionNameOptions"
                            placeholder="请选择检验项目"
                            v-model:bindValue="dataForm.innspectionName"
                            label="innspectionName"
                            value="id"
                            @change="GetEntryName()"
                        ></SHZY-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="检验条目" prop="entryName">
                        <SHZY-select
                            :list="entryNameOptions"
                            placeholder="请选择检验条目"
                            v-model:bindValue="dataForm.entryName"
                            label="innspectionName"
                            value="id"
                        ></SHZY-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="类型" prop="type">
                        <el-radio-group v-model="dataForm.type" size="small">
                            <el-radio-button
                                v-for="(item, index) in typeOptions"
                                :key="index"
                                :label="item.id"
                                >{{ item.fullName }}</el-radio-button
                            >
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="检验⽅法" prop="method">
                        <SHZY-select
                            :list="methodOptions"
                            placeholder="请选择检验⽅法"
                            v-model:bindValue="dataForm.method"
                            label="name"
                            value="id"
                        ></SHZY-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="单位" prop="unit">
                        <el-select
                            v-model="dataForm.unit"
                            placeholder="请选择"
                            clearable
                            :style="{ width: '100%' }"
                            filterable
                        >
                            <el-option
                                v-for="(item, index) in unitOptions"
                                :key="index"
                                :label="item.fullName"
                                :value="item.fullName"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="精度" prop="accuracy">
                        <el-input-number
                            v-model="dataForm.accuracy"
                            placeholder="数字文本"
                            :step="1"
                            :max="6"
                            :min="0"
                        >
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="有效标志" prop="enabledMark">
                        <el-switch
                            v-model="dataForm.enabledMark"
                            :active-value="1"
                            :inactive-value="0"
                        >
                        </el-switch>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                type="primary"
                @click="dataFormSubmit()"
                :loading="btnLoading"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>
<script>
import {
    getInnspectiondefaultInfo,
    createInnspectiondefault,
    updateInnspectiondefault
} from "@/api/q1/basedata/innspectionDefault";
import restAPI from "@/api/q1/basedata/innspectionEntry";
export default {
    components: {},
    props: [],
    data() {
        return {
            btnLoading: false,
            loading: false,
            visible: false,
            dataForm: {
                id: "",
                innspectionName: undefined,
                entryName: undefined,
                type: "ration",
                method: undefined,
                unit: undefined,
                accuracy: 1,
                enabledMark: 1
            },
            rules: {
                innspectionName: [
                    {
                        required: true,
                        message: "请选择检验项目",
                        trigger: '"change"'
                    }
                ],
                entryName: [
                    {
                        required: true,
                        message: "请选择检验条目",
                        trigger: '"change"'
                    }
                ],
                accuracy: [
                    {
                        required: true,
                        message: "请输入精度",
                        trigger: "blur"
                    },
                    {
                        validator: (rule, value, callback) => {
                            if (Number.isInteger(value)) {
                                callback();
                            } else {
                                callback(new Error("必须为整数"));
                            }
                        },
                        trigger: "blur"
                    }
                ],
                method: [
                    {
                        required: true,
                        message: "请选择检验⽅法",
                        trigger: '"change"'
                    }
                ]
            },
            innspectionNameOptions: [],
            entryNameOptions: [],
            typeOptions: [
                { fullName: "定量", id: "ration" },
                { fullName: "定性", id: "qualitative" }
            ],
            methodOptions: [],
            unitOptions: []
        };
    },
    computed: {},
    watch: {},
    created() {
        this.getBaseData();
    },
    mounted() {},
    methods: {
        GetEntryName() {
            restAPI.GetItemsInfo(this.dataForm.innspectionName).then(res => {
                if (res.data.list.length == 0) {
                    this.dataForm["entryName"] = undefined;
                }
                this.entryNameOptions = res.data.list;
            });
        },
        getBaseData() {
            this.$store
                .dispatch("base/getDictionaryData", { sort: "InspectionUnit" })
                .then(res => {
                    this.unitOptions = res;
                });
            this.$store.dispatch("qms/getInnspectionItemsData").then(res => {
                this.innspectionNameOptions = res;
            });
            // this.$store.dispatch('qms/getInnspectionEntryData').then((res) => {
            // 	this.entryNameOptions = res
            // })
            this.$store.dispatch("qms/getInnspectionMethodData").then(res => {
                this.methodOptions = res;
            });
        },
        goBack() {
            this.$emit("refresh");
        },
        init(id) {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["elForm"].resetFields();
                if (this.dataForm.id) {
                    getInnspectiondefaultInfo(this.dataForm.id).then(res => {
                        restAPI
                            .GetItemsInfo(res.data.innspectionName)
                            .then(res => {
                                if (res.data.list.length == 0) {
                                    this.dataForm["entryName"] = undefined;
                                }
                                this.entryNameOptions = res.data.list;
                            });
                        this.dataForm = res.data;
                    });
                }
            });
            //this.$store.commit('generator/UPDATE_RELATION_DATA', {})
        },
        dataFormSubmit() {
            this.$refs["elForm"].validate(valid => {
                if (valid) {
                    this.btnLoading = true;
                    const formMethod = !this.dataForm.id
                        ? createInnspectiondefault
                        : updateInnspectiondefault;
                    formMethod(this.dataForm)
                        .then(res => {
                            this.$message({
                                message: res.msg,
                                type: "success",
                                duration: 1000,
                                onClose: () => {
                                    this.btnLoading = false;
                                    (this.visible = false),
                                        this.$emit("refresh", true);
                                }
                            });
                        })
                        .catch(res => {
                            this.btnLoading = false;
                        });
                }
            });
        }
    }
};
</script>
